package YuanFuDao;

import java.util.Scanner;

/**
 */
public class Main2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            if(n==0){
                System.out.println(0);
            }else{
                int[][] nums = new int[n][2];
                for (int i = 0; i < n; i++) {
                    for (int j = 0; j < 2; j++) {
                        nums[i][j] = sc.nextInt();
                    }
                }
                int[] dp = new int[n];
                for (int i = 0; i < n; i++) {
                    if(nums[i][1] == 0){
                        dp[i] = nums[i][0];
                    }else{
                        //if(nums[i][0] > 0){
                            dp[i] = nums[i][0] + dp[nums[i][1]-2];
                        //}
                    }

                }
                int max = Integer.MIN_VALUE;
                for (int e : dp){
                    max = Math.max(max,e);
                }
                System.out.println(max);
            }
        }
    }


}
